﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using System.Reflection;
using skags.Code;
using skags.Models;

namespace skags.Controllers
{
    [HandleError]
    [LogError]
    public class SearchController : Controller
    {
        private DBRepository repository = new DBRepository();
        private string m_imgLocation;
        
        /* GET: /Search/Activities
         * Leitarvélin okkar, leitar í greinum, fréttum, tenglum og notendum
         */
        public JsonResult Activities(string q, int limit)
        {
            Regex regex = new Regex(@"(" + q + ")", RegexOptions.IgnoreCase);

            IList<ActivitySearchResultItem> results = new List<ActivitySearchResultItem>();
            var searchResult = repository.Search(q);
            foreach (var aItem in searchResult.Article)
            {
                m_imgLocation = "pencil.png";
                results.Add(new ActivitySearchResultItem(aItem.Title, Url.Action("../Article/ViewItem/", new { ID = aItem.ID }), m_imgLocation));
            } 
            foreach (var nItem in searchResult.News)
            {
                m_imgLocation = "news.png";
                results.Add(new ActivitySearchResultItem(nItem.Title, Url.Action("../News/ViewItem/", new { ID = nItem.ID }), m_imgLocation));
            }
            foreach (var lItem in searchResult.Links)
            {
                m_imgLocation = "links.png";

                results.Add(new ActivitySearchResultItem(lItem.Title, lItem.Url, m_imgLocation));
            }
            foreach (var fItem in searchResult.Forum)
            {
                m_imgLocation = "chat.png";

                results.Add(new ActivitySearchResultItem(fItem.Title, Url.Action("../Forum/Thread/", new { ID = fItem.ID }), m_imgLocation));
            }
            /* Skilar niðurstöðum á JSON formi */
            return Json(results, JsonRequestBehavior.AllowGet);
        }
    }
}

